﻿using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PlayCardCapture
{
    public partial class ResultForm : Form
    {
        //private long frameCount;
        private Emgu.CV.UI.ImageBox selectBox;
        private Holder selectHolder;
        private ImageManager manager;
        private int page = 0;
        private TableLayoutPanel secondLayoutPanel;
        public ResultForm()
        {
            InitializeComponent();
            
            //IntPtr image = CvInvoke.cvLoadImage(@"D:\icon\miaomiao.jpg", Emgu.CV.CvEnum.LOAD_IMAGE_TYPE.CV_LOAD_IMAGE_ANYCOLOR);
            //Image<Bgr, byte> dest = new Image<Bgr, byte>(CvInvoke.cvGetSize(image));
            //CvInvoke.cvCopy(image, dest, IntPtr.Zero);
        }

        private void initTable()
        {
            tableLayoutPanel2.ColumnStyles.Clear();
            tableLayoutPanel2.ColumnCount = manager.PlayerCount < 5 ? manager.PlayerCount : 5;
            for (int i = 0; i < tableLayoutPanel2.ColumnCount; i++)
                tableLayoutPanel2.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100f));
            
            tableLayoutPanel1.RowCount = manager.CardPerPlayer;
            tableLayoutPanel1.ColumnCount = manager.PlayerCount < 5 ? manager.PlayerCount : 5;
            tableLayoutPanel1.RowStyles.Clear();
            tableLayoutPanel1.ColumnStyles.Clear();

            for (int i = 0; i < tableLayoutPanel1.ColumnCount; i++)
                tableLayoutPanel1.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100f));
            for (int i = 0; i < tableLayoutPanel1.RowCount; i++)
                tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.Percent, 100f));

            if (manager.PlayerCount > 5)
                initSecondeLayout();
        }

        private void initSecondeLayout()
        {
            secondLayoutPanel = new TableLayoutPanel();
            secondLayoutPanel.ColumnCount = 5;
            secondLayoutPanel.Dock = System.Windows.Forms.DockStyle.Fill;
            secondLayoutPanel.Location = new System.Drawing.Point(0, 0);
            secondLayoutPanel.Name = "tableLayoutPanel3";
            secondLayoutPanel.RowCount = manager.CardPerPlayer;

            for (int i = 0; i < secondLayoutPanel.ColumnCount; i++)
                secondLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100f));
            for (int i = 0; i < secondLayoutPanel.RowCount; i++)
                secondLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Percent, 100f));
        }

        private void initTitle()
        {
            tableLayoutPanel2.Controls.Clear();
            if(page == 0)
                for (int i = 0; i < (manager.PlayerCount < 5 ? manager.PlayerCount : 5); i++)
                {
                    Label label2 = new Label();
                    label2.Text = "玩家"+(page*5 + i+1);
                    label2.Font = new System.Drawing.Font("宋体", 12);
                    label2.Dock = DockStyle.Fill;
                    label2.TextAlign = ContentAlignment.MiddleCenter;
                    tableLayoutPanel2.Controls.Add(label2, i, 0);
                }
            else
                for (int i = 0; i < manager.PlayerCount - 5; i++)
                {
                    Label label2 = new Label();
                    label2.Text = "玩家" + (page * 5 + i + 1);
                    label2.Font = new System.Drawing.Font("宋体", 12);
                    label2.Dock = DockStyle.Fill;
                    label2.TextAlign = ContentAlignment.MiddleCenter;
                    tableLayoutPanel2.Controls.Add(label2, i, 0);
                }
        }

        public void init(ImageManager manager)
        {
            this.manager = manager;
            initTable();
            initTitle();
            initImageBox();
            if (manager.PlayerCount <= 5)
                button2.Enabled = false;
        }

        public void initImageBox()
        {
            for (int i = 0; i < tableLayoutPanel1.RowCount; i++)
                for (int j = 0; j < tableLayoutPanel1.ColumnCount; j++)
                {
                    Emgu.CV.UI.ImageBox imageBox = new Emgu.CV.UI.ImageBox();
                    imageBox.Image = manager.getImage(i,j);
                    imageBox.Dock = DockStyle.Fill;
                    imageBox.Click += imageBoxClick;
                    imageBox.BorderStyle = BorderStyle.FixedSingle;
                    imageBox.Tag = new Holder(i,j);
                    //imageBox.BackColor = System.Drawing.SystemColors.ActiveCaption;
                    imageBox.SizeMode = PictureBoxSizeMode.Zoom;
                    tableLayoutPanel1.Controls.Add(imageBox, j, i);
                }
            if (manager.PlayerCount > 5)
                initSecondImageBox();
        }

        private void initSecondImageBox()
        {
            for (int i = 0; i < secondLayoutPanel.RowCount; i++)
                for (int j = 0; j < manager.PlayerCount - 5; j++)
                {
                    Emgu.CV.UI.ImageBox imageBox = new Emgu.CV.UI.ImageBox();
                    imageBox.Image = manager.getImage(i, 5+j);
                    imageBox.Dock = DockStyle.Fill;
                    imageBox.Click += imageBoxClick;
                    imageBox.BorderStyle = BorderStyle.FixedSingle;
                    imageBox.Tag = new Holder(i, 5+j);
                    //imageBox.BackColor = System.Drawing.SystemColors.ActiveCaption;

                    secondLayoutPanel.Controls.Add(imageBox, j, i);
                }
        }

        public void imageBoxClick(object sender, EventArgs e)
        {
            MouseEventArgs me = e as MouseEventArgs;
            Emgu.CV.UI.ImageBox imageBox = sender as Emgu.CV.UI.ImageBox;
            Holder holder = imageBox.Tag as Holder;
            switch(me.Button)
            {
                case MouseButtons.Left:
                    selectBox = imageBox;
                    selectHolder = holder;
                    break;
                case MouseButtons.Middle:
                    manager.reset(holder.i,holder.j);
                    imageBox.Image = manager.getImage(holder.i, holder.j);
                    break;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void ResultForm_Shown(object sender, EventArgs e)
        {
            //int[] indexArray = new int[1];
            //indexArray[0] = 10;
            //ImageManager manager = new SingleFileManager(@"E:\film\recycles\1211111.avi", 1, 1, indexArray);
            //init(manager);
        }

        private void ResultForm_KeyDown(object sender, KeyEventArgs e)
        {
            if(selectBox != null)
            {
                if(e.KeyCode == Keys.Left)
                {
                    manager.moveFore(selectHolder.i, selectHolder.j);
                    selectBox.Image = manager.getImage(selectHolder.i, selectHolder.j);
                }
                else if(e.KeyCode == Keys.Right)
                {
                    manager.moveBack(selectHolder.i, selectHolder.j);
                    selectBox.Image = manager.getImage(selectHolder.i, selectHolder.j);
                }
            }
        }

        private void ResultForm_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (selectBox != null)
            {
                if(e.KeyChar == ',')
                {
                    manager.moveFore(selectHolder.i, selectHolder.j);
                    selectBox.Image = manager.getImage(selectHolder.i, selectHolder.j);
                }
                else if (e.KeyChar == '.')
                {
                    manager.moveBack(selectHolder.i, selectHolder.j);
                    selectBox.Image = manager.getImage(selectHolder.i, selectHolder.j);
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            splitContainer1.Panel2.Controls.Remove(secondLayoutPanel);
            splitContainer1.Panel2.Controls.Add(tableLayoutPanel1);
            page = 0;
            initTitle();
            button2.Enabled = true;
            button1.Enabled = false;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            splitContainer1.Panel2.Controls.Remove(tableLayoutPanel1);
            splitContainer1.Panel2.Controls.Add(secondLayoutPanel);
            page = 1;
            initTitle();
            button2.Enabled = false;
            button1.Enabled = true;
        }
    }

    class Holder
    {
        public Holder(int i, int j)
        {
            this.i = i;
            this.j = j;
        }
        public int i;
        public int j;
    }
}
